
const { PlayerModel } = require("../../model/playerModel");
// 生成玩家
cc.Class({
    extends: cc.Component,

    properties: {
        playerPos: {
            type: [cc.Node],
            default: [],
        },
        playerPrefab: {
            type: cc.Prefab,
            default: null,
        },
        // 玩家头像
        playerPhotos: {
            type: [cc.SpriteFrame],
            default: [],
        },
        // 庄家特效
        dealerFx: {
            type: cc.Node,
            default: null,
        },
        // 庄家卡牌背景
        dealerCardBG: {
            type: cc.Node,
            default: null,
        },
        // 庄家卡牌文本
        dealerCardText: {
            type: cc.Node,
            default: null,
        }

    },

    // 设置控制器
    initWithController: function(controller) {
        this.controller = controller;
        // 所有的玩家
        this.allPlayerView = [];
        // 隐藏庄家
        this.dealerFx.active = false;
        this.dealerCardBG.active = false;
        this.dealerCardText.active = false;
        this.playerIndex = 2;
    },

    // 创建玩家
    createPlayers() {
        for(let i = 0; i < this.playerPos.length; ++i) {
            const pos = this.playerPos[i];
            // player0 上没有info组件，info是 player0下的一个子节点
            const playerInfo = pos.getChildByName("info").getPosition();
            const stakeOnTable = pos.getChildByName("stake").getPosition();
            const m = new PlayerModel(i);
            const playerPrefab = cc.instantiate(this.playerPrefab);
            const playerView = playerPrefab.getComponent("playerView");
            playerView.initWithModel(m, playerInfo, stakeOnTable, this.playerPhotos[i]);
            playerView.updateStake();
            // 添加节点
            pos.addChild(playerPrefab);
            this.allPlayerView.push(playerView);
        }
    },

    // 本玩家
    getPlayer() {
        return this.allPlayerView[this.playerIndex];
    },
});
